package com.wutong.xsxx.xjxx.service.impl;

import java.util.List;
import com.wutong.common.utils.DateUtils;
import com.wutong.common.utils.uuid.IdUtils;
import com.wutong.xsxx.xjxx.domain.Xjxx;
import com.wutong.xsxx.xjxx.domain.XjxxFj;
import com.wutong.xsxx.xjxx.mapper.XjxxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.wutong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.wutong.xsxx.xjxx.service.IXjxxService;

import static com.wutong.common.utils.SecurityUtils.*;

/**
 * 学籍信息Service业务层处理
 * 
 * @author wutong
 * @date 2025-05-15
 */
@Service
public class XjxxServiceImpl implements IXjxxService 
{
    @Autowired
    private XjxxMapper xjxxMapper;

    /**
     * 查询学籍信息
     * 
     * @param xjxjId 学籍信息主键
     * @return 学籍信息
     */
    @Override
    public Xjxx selectXjxxByXjxjId(String xjxjId)
    {
        return xjxxMapper.selectXjxxByXjxjId(xjxjId);
    }

    /**
     * 查询学籍信息列表
     * 
     * @param xjxx 学籍信息
     * @return 学籍信息
     */
    @Override
    public List<Xjxx> selectXjxxList(Xjxx xjxx)
    {
        return xjxxMapper.selectXjxxList(xjxx);
    }

    /**
     * 新增学籍信息
     * 
     * @param xjxx 学籍信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertXjxx(Xjxx xjxx)
    {
        xjxx.setCreateTime(DateUtils.getNowDate());
        xjxx.setXjxjId(IdUtils.fastSimpleUUID());
        xjxx.setCreateTime(DateUtils.getNowDate());
        xjxx.setCreateBy(getUsername());
        xjxx.setUserId(getUserId());
        xjxx.setDeptId(getDeptId());
        int rows = xjxxMapper.insertXjxx(xjxx);
        insertXjxxFj(xjxx);
        return rows;
    }

    /**
     * 修改学籍信息
     * 
     * @param xjxx 学籍信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateXjxx(Xjxx xjxx)
    {
        xjxx.setUpdateTime(DateUtils.getNowDate());
        xjxxMapper.deleteXjxxFjByXjxxId(xjxx.getXjxjId());
        insertXjxxFj(xjxx);
        return xjxxMapper.updateXjxx(xjxx);
    }

    /**
     * 批量删除学籍信息
     * 
     * @param xjxjIds 需要删除的学籍信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXjxxByXjxjIds(String[] xjxjIds)
    {
        xjxxMapper.deleteXjxxFjByXjxxIds(xjxjIds);
        return xjxxMapper.deleteXjxxByXjxjIds(xjxjIds);
    }

    /**
     * 删除学籍信息信息
     * 
     * @param xjxjId 学籍信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXjxxByXjxjId(String xjxjId)
    {
        xjxxMapper.deleteXjxxFjByXjxxId(xjxjId);
        return xjxxMapper.deleteXjxxByXjxjId(xjxjId);
    }

    /**
     * 新增学籍附件信息
     * 
     * @param xjxx 学籍信息对象
     */
    public void insertXjxxFj(Xjxx xjxx)
    {
        List<XjxxFj> xjxxFjList = xjxx.getXjxxFjList();
        String xjxjId = xjxx.getXjxjId();
        if (StringUtils.isNotNull(xjxxFjList))
        {
            List<XjxxFj> list = new ArrayList<XjxxFj>();
            for (XjxxFj xjxxFj : xjxxFjList)
            {
                xjxxFj.setXjxxId(xjxjId);
                list.add(xjxxFj);
            }
            if (list.size() > 0)
            {
                xjxxMapper.batchXjxxFj(list);
            }
        }
    }
}
